<?php /**
 * 代码生成器生成
 * @author: jithink
 */
namespace Admin\Controller;

class AskController extends CommonController {

    public function answerList(){
        $id = I('id');

        $where = array();
        $where['ask_id'] = $id;
        $answerList = $this->lists('Answer', $where);

        FPC($answerList);
        $this->assign('list', $answerList);
        $this->display();
    }

    public function toggleStatusForAnswer(){

        $id = I('request.id');
        $askId = I('request.ask_id');
        if ($id) {
            $data = M('Answer')->find($id);
            $data['id'] = $id;
            if ($data['status'] == 1) {
                $data['status'] = 0;
                $msg = '待定';
            } else {
                $res = M('Answer')->where(array('ask_id'=>$askId))->save(array('status'=>0));
                # 加积分
                $askData = M('Ask')->find($askId);
                $result = M('Member')->where(array('uid'=>$data['uid']))->setInc('score',$askData['score']);
                FPC();
                # END-加积分
                $data['status'] = 1;
                $msg = '最佳';
            }
            $res = M('Answer')->save($data);
            if($res == false)
            {
                $this->mtReturn(200, $msg.'操作失败!', $_REQUEST['navTabId'], false);
            }
            $this->mtReturn(200, $msg.'操作成功!', $_REQUEST['navTabId'], false);
        }
        $this->mtReturn(300, '操作失败!', $_REQUEST['navTabId'], false);
    }
    public function answer(){
        $content = I('content');//回答内容
        $id = I('id');//问题ID
        if(IS_POST){
            $data['content'] = $content;
            $User = M('Answer');
            $where = array();
            $where['ask_id'] = $id;
            $answerList = $this->lists('Answer', $where);
            if($answerList) {//判断当前问题是否已经回答
                $User->where($where)->save($data);
            }else{

                $User->create();
                $User->content = $content;
                $User->id = null;
                $User->uid = UID;
                $User->ask_id = $id;
                $User->create_time = time();
                $User->status = 1;
                $User->add();
            }
            $this->success('编辑成功！');
        } else {
            $where = array();
            $where['ask_id'] = $id;
            $answerList = $this->lists('Answer', $where);
            $this->assign('answer',$answerList);
            var_dump($answerList);
            $this->display();
        }
    }

}